package org.cllib.ds;

public class Stack {
	Object[] objs;
	public static int INITIAL_CAPACITY = 8;
	public static int CAPACITY_STEP = 8;
	
	protected int count;
	protected int capacity;
	
	public void push(Object obj){
		if(count >= capacity){
			Object[] temp = new Object[count + CAPACITY_STEP];
			if(objs != null){
				System.arraycopy(objs, 0, temp, 0, count);				
			}
			capacity += CAPACITY_STEP;
			objs = temp;
			temp = null;
		}
		objs[count ++] = obj;
	}
	
	public Object pop(){
		if(isEmpty()){
			return null;
		}
		
		return objs[--count];
	}
	
	public boolean isEmpty(){
		return count <= 0;
	}
	
	public Object[] snapshot(){
		if(isEmpty()){
			return new Object[0];
		}
		
		Object[] ts = new Object[count];
		System.arraycopy(objs, 0, ts, 0, count);
		return ts;
	}
}
